//
//  XHEditorPassWordViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/17.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHEditorPassWordViewController.h"
#import "XHTabBarController.h"

#import "AppDelegate.h"
#import "Services.h"

@interface XHEditorPassWordViewController ()
@property (weak, nonatomic) IBOutlet UIView *ui_authCodeView;
@property (weak, nonatomic) IBOutlet UIView *ui_passWordView;
@property (weak, nonatomic) IBOutlet UIView *ui_passWordView2;
@property (weak, nonatomic) IBOutlet UIView *ui_getAuthView;
@property (weak, nonatomic) IBOutlet UITextField *ui_codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *ui_passWordTextField;
@property (weak, nonatomic) IBOutlet UITextField *ui_passWordAgainTextField;

@property (weak, nonatomic) IBOutlet UIButton *ui_button;
@property (weak, nonatomic) IBOutlet UIButton *ui_codeButton;

@property (strong, nonatomic) NSTimer *m_theTimer;
@property (assign, nonatomic) int waitingTime;
@property (strong, nonatomic) NSDictionary *m_smsResultDic;

@end

@implementation XHEditorPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"修改密码";
    self.ui_authCodeView.layer.borderWidth = 1;
    self.ui_authCodeView.layer.borderColor = [ColorManager colorWithHexString:@"#C4C4C4"].CGColor;
    
    self.ui_passWordView.layer.borderWidth = 1;
    self.ui_passWordView.layer.borderColor = [ColorManager colorWithHexString:@"#C4C4C4"].CGColor;
    
    self.ui_passWordView2.layer.borderWidth = 1;
    self.ui_passWordView2.layer.borderColor = [ColorManager colorWithHexString:@"#C4C4C4"].CGColor;
    
    self.ui_getAuthView.layer.borderWidth = 1;
    self.ui_getAuthView.layer.borderColor = [ColorManager colorWithHexString:@"#C4C4C4"].CGColor;
    
    self.ui_button.layer.masksToBounds = YES;
    self.ui_button.layer.cornerRadius = 8;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)showSmsCodeWaitView{
    self.ui_codeButton.enabled=NO;
    self.waitingTime=60;
    [self.ui_codeButton setTitle:[NSString stringWithFormat:@"%d秒后重发",self.waitingTime] forState:UIControlStateDisabled];
    self.m_theTimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateSmsCodeWait) userInfo:nil repeats:YES];
}

- (void)updateSmsCodeWait
{
    self.waitingTime--;
    NSString *title=[NSString stringWithFormat:@"%d秒后重发",self.waitingTime];
    self.ui_codeButton.titleLabel.text=title;
    [self.ui_codeButton setTitle:title forState:UIControlStateDisabled];
    if (self.waitingTime<=0) {
        [self hideSmsCodeWaitView];
    }
}

- (void)hideSmsCodeWaitView{
    [self.m_theTimer invalidate];
    [self.ui_codeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
    self.ui_codeButton.enabled=YES;
}

- (IBAction)getCodeAction:(id)sender
{
    [self showSmsCodeWaitView];
    [self.ui_codeTextField becomeFirstResponder];
    NSString * phone = @"";
    if ([NSString checkNullString:[DataManager sharedInstance].userId])
    {
        phone=self.phone;
    }
    else
    {
        phone=[DataManager sharedInstance].userId;
    }
    [ServiceManager getMessageCodeWithPhone:phone success:^(NSDictionary *data) {
        self.m_smsResultDic=data;
        DLog(@"发送短信验证码：成功");
        [PromptAction showSuccessPrompt:@"验证码成功发送"];
    } failure:^{
        DLog(@"发送短信验证码：失败");
    }];
}


- (IBAction)changePassWordAction:(id)sender
{
    if ([self isCanPush])
    {
        NSString *smsId=self.m_smsResultDic[@"smsCode"];
        NSString * phone = @"";
        if ([NSString checkNullString:[DataManager sharedInstance].userId])
        {
            phone=self.phone;
        }
        else
        {
            phone=[DataManager sharedInstance].userId;
        }

        [ServiceManager changePassWord:self.ui_passWordTextField.text code:self.ui_codeTextField.text phone:phone smsId:smsId success:^(NSDictionary *data) {
            [PromptAction showSuccessPrompt:@"密码修改成功"];
            [[DataManager sharedInstance] clearUserDefaults];
            AppDelegate * delegate = [UIApplication sharedApplication].delegate;
            [delegate.tabBarController goLoginViewController];
            [self.navigationController popToRootViewControllerAnimated:YES];

        } failure:^{
            [PromptAction showInfoPrompt:@"密码修改失败"];
        }];
    }
}

- (BOOL)isCanPush
{
    if([NSString checkNullString:self.ui_codeTextField.text])
    {
        [PromptAction showInfoPrompt:@"请输入验证码"];
        return NO;
    }
    if([NSString checkNullString:self.ui_passWordTextField.text])
    {
        [PromptAction showInfoPrompt:@"请输入密码"];
        return NO;
    }
    if([NSString checkNullString:self.ui_passWordAgainTextField.text])
    {
        [PromptAction showInfoPrompt:@"请再次输入密码"];
        return NO;
    }
    if (![self.ui_passWordTextField.text isEqualToString:self.ui_passWordAgainTextField.text])
    {
        [PromptAction showInfoPrompt:@"密码前后输入不一致"];
        return NO;
    }
    if(self.ui_passWordTextField.text.length<6)
    {
        [PromptAction showErrorPrompt:@"密码长度至少6位"];
        return NO;
    }
    
    return YES;
}

- (IBAction)clickBgView:(id)sender
{
    [self.view endEditing:YES];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.ui_passWordTextField) {
        if (textField.text.length > 17) return NO;
    }
    if (textField == self.ui_passWordAgainTextField) {
        if (textField.text.length > 17) return NO;
    }
    
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
